#include "Button.h"

Button::Button(SDL_Rect rect, string str, SDL_Color color)
{
    this->m_rect = rect;
    this->m_name = str;
    this->color  =color;
    this->isFocus=false;
    this->isClicked=false;
}

SDL_Color Button::getColor() const
{
    return color;
}

void Button::setColor(const SDL_Color& color)
{
    this->color = color;
}

SDL_Rect Button::getRect() const
{
    return m_rect;
}

string Button::getName() const
{
    return m_name;
}

bool Button::mousePressEvent(SDL_Event* msg)
{
    if (contains(msg->motion.x,msg->motion.y))
    {
        isFocus = true;
        if (msg->type==SDL_MOUSEBUTTONDOWN)
        {
            isClicked = true;
            return true;
        }else
        {
            isFocus=false;
        }
    }
    isFocus=false;
    return false;
}

bool Button::contains(int x, int y)
{
    int lx = m_rect.x;
    int ly = m_rect.y;
    int rx = lx+m_rect.w;
    int ry= ly + m_rect.h;
    return  x > lx && x<rx && y>ly && y < ry;
}

void Button::render(SDL_Renderer* render, ButtonType type)
{
    if (type==ButtonType::ColorTool)
    {
        SDL_SetRenderDrawColor(render,color.r,color.g,color.b,0);
        SDL_RenderFillRect(render,&m_rect);
        if (isFocus==false)
        {
            SDL_SetRenderDrawColor(render,160,160,160,0);

        }else
        {
            SDL_SetRenderDrawColor(render,100,165,231,0);
        }
        SDL_Rect dst = {m_rect.x-INTERVAL,m_rect.y-INTERVAL ,m_rect.w+2*INTERVAL,m_rect.h+2*INTERVAL};
        SDL_RenderDrawRect(render,&dst);

    }else if (type==Menu)
    {
        if (isClicked==true)
        {
            SDL_SetRenderDrawColor(render,98,162,228,0);
            SDL_RenderDrawRect(render,&m_rect);
            SDL_SetRenderDrawColor(render,201,224,247,0);
            SDL_Rect dst = {m_rect.x+INTERVAL,m_rect.y+INTERVAL,40-4,66-4};
            SDL_RenderFillRect(render,&dst);

        }else if (isFocus==true)
        {
            SDL_SetRenderDrawColor(render,164,206,249,0);
            SDL_RenderDrawRect(render,&m_rect);
            SDL_SetRenderDrawColor(render,232,239,247,0);
            SDL_Rect dst = {m_rect.x+INTERVAL,m_rect.y+INTERVAL,40-4,66-4};
            SDL_RenderFillRect(render,&dst);
        }

        //颜色框

        SDL_Rect r1 ={m_rect.x+(m_rect.w-30)/2,10,30,30};
        SDL_Rect r2 = {r1.x-INTERVAL,r1.y-INTERVAL,r1.w+2*INTERVAL,r1.h+2*INTERVAL};
        SDL_SetRenderDrawColor(render,0,0,0,0);
        SDL_RenderFillRect(render,&r2);
        SDL_SetRenderDrawColor(render,color.r,color.g,color.b,0);
        SDL_RenderFillRect(render,&r1);
    }
}
